package in.muton.edushare.auth;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.url.URLLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.protocol.http.WebRequest;

import com.edushare.common.dao.UsersDAO;
import com.edushare.common.util.CookieUtils;
import com.edushare.common.vo.Users;

public class LoginUsingCookie extends EduSharePage {

	private static final Log log = LogFactory.getLog(LoginUsingCookie.class);
	
	public LoginUsingCookie() {
		log.info("Trying to log-in using Cookie previously stored.");
		getSession().bind();
		String userName = CookieUtils.getUserNameFromCookie(
								((WebRequest)RequestCycle.get().getRequest()).getCookies()
															);
		Users u = new UsersDAO(getSessionORMInterface()).getUserFromUserName(userName);
		log.info("Logging in as "+ u.getUserName()+"["+u.getUserId()+"], based on cookie data.");
		EduShareSession.get().logInUser(u);
		
		log.debug("redirecting to some other page.");
		setResponsePage(URLLoader.getLandingPageClass(u.getLandingPageId()));
	}

}
